package com.detrum.sportcamera.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.detrum.base.BaseStatic;
import com.detrum.bean.FileBean;
import com.detrum.sportcamera.R;
import com.detrum.swipe.BaseRecyclerAdapter;
import com.detrum.utils.LogUtils;

import java.util.ArrayList;

/**
 * Created by abc on 2018/1/5.
 */

public class NfsRefreshAdapter extends BaseRecyclerAdapter<NfsRefreshAdapter.NfsHolder,FileBean> {
    private final String TAG = "NfsRefreshAdapter";
    private Context context;
    private ArrayList<FileBean> list_bean;
    private boolean isEdit,isAll;

    public NfsRefreshAdapter(ArrayList<FileBean> mDataList, Context context) {
        super(mDataList);
        list_bean = mDataList;
        this.context = context;
        Log.e(TAG,"list_bean size is "+list_bean.size());
    }

    public void setEdit(boolean isEdit) {
        this.isEdit = isEdit;
    }

    @Override
    public int getItemCount() {
        return list_bean == null ? 0 : list_bean.size();
    }

    @Override
    public NfsHolder createViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.nfsitemview, parent, false);
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) itemView.getLayoutParams();
        params.width = BaseStatic.SCREEN_WIDTH / 2;
        params.height = BaseStatic.SCREEN_WIDTH / 2;
        itemView.setLayoutParams(params);
        NfsHolder nfsHolder = new NfsHolder(itemView);
        return nfsHolder;
    }

    @Override
    public void onBindViewHolder(NfsHolder holder, int position, FileBean data) {
        String fileName = null;
        fileName = data.getFileName();
        holder.item_name.setText(fileName);
        boolean isSelect = data.isSelect();
        LogUtils.i(position + "----===----" + data.getCachePath());
        if (isEdit) {
            holder.select_iv.setVisibility(View.VISIBLE);
            if (isSelect) {
                holder.select_iv.setImageResource(R.mipmap.select);
            } else {
                holder.select_iv.setImageResource(R.mipmap.noselect);
            }
        } else {
            holder.select_iv.setVisibility(View.GONE);
            data.setSelect(false);
        }
        if (fileName.toLowerCase().endsWith("png") || fileName.toLowerCase().endsWith("jpg") || fileName.toLowerCase().endsWith("jpeg")) {
            holder.item_center_iv.setVisibility(View.GONE);
            holder.item_time.setVisibility(View.GONE);
            if (data.getCachePath() != null) {
                Glide.with(context).load(data.getCachePath()).error(R.mipmap.ic_launcher).into(holder.item_iv);
            }else{
                holder.item_iv.setImageResource(R.mipmap.wait);
            }
        } else {
            holder.item_center_iv.setVisibility(View.VISIBLE);
            holder.item_center_iv.setImageResource(R.mipmap.video_play);
            holder.item_time.setVisibility(View.VISIBLE);
            Log.e(TAG,position+"---------- time = "+data.getTime());
            if(data.getTime()!=null){
                holder.item_time.setText(data.getTime());
            }else{
                holder.item_time.setVisibility(View.GONE);
            }
            if(data.getThumbPath()!=null){
                Glide.with(context).load(data.getThumbPath()).error(R.mipmap.ic_launcher).into(holder.item_iv);
            }else {
                holder.item_iv.setImageResource(R.mipmap.loading);
            }
        }

    }

    class NfsHolder extends BaseRecyclerAdapter.BaseRecyclerViewHolder{
        private ImageView item_iv;
        private ImageView item_center_iv,select_iv;
        private TextView item_name,item_time;
        public NfsHolder(View itemView) {
            super(itemView);
            item_iv = itemView.findViewById(R.id.item_iv);
            item_center_iv = itemView.findViewById(R.id.item_center_iv);
            select_iv = itemView.findViewById(R.id.select_iv);
            item_name = itemView.findViewById(R.id.item_name);
            item_time = itemView.findViewById(R.id.item_time);
        }
    }
}
